package com.das.mechanic_webview.view.report;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.utils.X3EdittextWatcher;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.widget.X3MemberButtonView;
import com.das.mechanic_webview.R;
import com.das.mechanic_webview.contract.report.X3SendReprotContract;
import com.das.mechanic_webview.presenter.report.X3SendReportPresenter;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = X3AppRouterConstants.Router.SEND_REPORT)
public class X3SendReportActivity extends X3BaseActivity<X3SendReportPresenter> implements X3MemberButtonView.IOnClickAffirm, X3SendReprotContract.View {

    @BindView(R2.id.rl_parent)
    RelativeLayout rl_parent;
    @BindView(R2.id.et_name)
    EditText et_name;
    @BindView(R2.id.mv_view)
    X3MemberButtonView mv_view;
    @BindView(R2.id.tv_notice)
    TextView tv_notice;
    @BindView(R2.id.iv_empty)
    ImageView iv_empty;

    private long workBaseId;
    private boolean isAlone;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_activity_send_report;
    }

    @Override
    protected void initView() {
        rl_parent.setPadding(0, X3StatusBarUtil.getStatusBarHeight(this),0,0);
        X3StatusBarUtil.darkMode(this);
        workBaseId = getIntent().getLongExtra("workBaseId", 0L);
        isAlone = getIntent().getBooleanExtra("isAlone", false);

        tv_notice.setText(isAlone ? getString(R.string.send_reprot_notice) : getString(R.string.send_work_reprot_notice));
        et_name.addTextChangedListener(new X3EdittextWatcher() {
            @Override
            protected void textInputAfter(String content) {
                mv_view.changeBtnStatus(!X3StringUtils.isEmpty(content));
                iv_empty.setVisibility(!X3StringUtils.isEmpty(content) ? View.VISIBLE : View.GONE);
            }
        });
        mv_view.setiOnClickAffirm(this);
        if (mPresenter != null) {
            mPresenter.getCarOwnerMobile(workBaseId);
        }

    }

    @Override
    protected X3SendReportPresenter createPresenter() {
        return new X3SendReportPresenter();
    }

    @OnClick({R2.id.iv_back,R2.id.iv_empty})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.iv_empty) {
            et_name.setText("");
        }
    }

    @Override
    public void iOnClickAffirm() {
        if (isAlone) {
            if (mPresenter != null) {
                mPresenter.sendReport(workBaseId,et_name.getText().toString());
            }
        }else {
            if (mPresenter != null) {
                mPresenter.sendWorkReport(workBaseId,et_name.getText().toString());
            }
        }
    }

    @Override
    public void sendSuccess() {
        X3ToastUtils.showMessage(getString(com.das.mechanic_base.R.string.x3_send_live_success));
        finish();
    }

    @Override
    public void getCarOwnerMobileSuccess(String mobile) {
        if (X3StringUtils.isEmpty(mobile)) {
            return;
        }
        et_name.setText(mobile);
        et_name.setSelection(mobile.length());

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("发送检测报告输入手机号页");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("发送检测报告输入手机号页");
    }
}